package com.zfp.hs.controller;


import com.zfp.core.controller.BaseController;
import com.zfp.core.plugins.page.Page;
import com.zfp.hs.dao.OrderDao;
import com.zfp.hs.entity.Hospital;
import com.zfp.hs.entity.Order;
import com.zfp.hs.service.HospitalService;
import com.zfp.sys.entity.SysRole;
import com.zfp.sys.entity.SysUser;
import com.zfp.sys.service.SysRoleService;
import com.zfp.sys.service.SysUserService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Controller
@RequestMapping("hs/hospital/")
public class HospitalController extends BaseController {
	@Autowired
	private HospitalService hospitalService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private OrderDao orderDao;
    private final String saveDoctorBelonged = "saveDoctorBelonged";

    @RequestMapping("pageList")
	public String hospitalList(Page<Hospital> page, Hospital hospital, Model model,String isfront){
		if (page.getShowCount() == 20) {
			page.setShowCount(13); // 初始页面每页几行自定义
		}
		page=hospitalService.pageList(page, hospital);
		model.addAttribute("page", page);
		model.addAttribute("hospital", hospital);
        model.addAttribute("isfront",isfront);
		return "hs/hospitalList";
	}

    @RequestMapping("turnSave")
    public String turn(String id,Model model){
        if(id != null && id != ""){
            Hospital hospital=hospitalService.getById("Hospital.getById", id);
            model.addAttribute("hospital",hospital);
        }
        return "hs/hospitalSavePage";
    }
    @RequestMapping("save")
    public void save(PrintWriter out, Hospital hospital){
        //System.out.println(sysUser.getUserId());
        Boolean b = hospitalService.save(hospital);
        //System.out.println(b);
        out.print(b);
        out.close();
    }

    @RequestMapping("delete")
    public void delete(PrintWriter out,Hospital hospital){

        Boolean b = hospitalService.deleteById(hospital);
        out.print(b);
        out.close();
    }

    @RequestMapping("departmentList")
    public String departmentList(Page<Hospital> page, Hospital hospital, Model model,String isfront){
        if (page.getShowCount() == 20) {
            page.setShowCount(13); // 初始页面每页几行自定义
        }
        page=hospitalService.pageList("Hospital.findDepartmentList",page, hospital);
        model.addAttribute("page", page);

        try {
            hospital.setName(java.net.URLDecoder.decode(hospital.getName(),"UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        model.addAttribute("hospital", hospital);
        model.addAttribute("isfront",isfront);
        return "hs/departmentList";
    }


    @RequestMapping("departmentTurnSave")
    public String departmentTurnSave(Hospital hospital,Model model){
        if(hospital.getDepartmentid() != null ){
            Hospital department=hospitalService.getById("Hospital.getdepartmentById", hospital.getDepartmentid().toString());
            model.addAttribute("department",department);
        }
        model.addAttribute("hospital",hospital);
        return "hs/departmentSavePage";
    }
    @RequestMapping("departmentSave")
    public void departmentSave(PrintWriter out, Hospital hospital){
        //System.out.println(sysUser.getUserId());
        Boolean b = hospitalService.saveDepartment(hospital);
        //System.out.println(b);
        out.print(b);
        out.close();
    }

    @RequestMapping("departmentDelete")
    public void departmentDelete(PrintWriter out,Hospital hospital){

        Boolean b = hospitalService.deleteDepartmentById(hospital);
        out.print(b);
        out.close();
    }

    @RequestMapping("doctorList")
    public String doctorList(String isfront,HttpSession session, SysUser sysUser, Page<SysUser> page, Model model, String isall){
        if (page.getShowCount() == 20) {
            page.setShowCount(13); // 初始页面每页几行自定义
        }
        SysUser sysUserSession = (SysUser) session.getAttribute("sessionUser");
        List<SysRole> roles = sysRoleService.list("SysRole.findUserRoles",sysUserSession.getUserId());
        String isadmin = "0";
        String isdoctor = "0";
        for(SysRole role : roles){
            if(role.getRoleId() == 4){
                isdoctor="1";
            }
            if (role.getRoleId() == 1 || role.getRoleId() == 2){
                isadmin="1";
            }
        }
        if(!isadmin.equals("1") && isdoctor.equals("1") ){
            model.addAttribute("role","doctor");
        }
        page=sysUserService.pageList("SysUser.doctorList",page,sysUser);
        Order order = new Order();
        for(SysUser sysUser1 : page.getResult()){
            order.setDoctorid(sysUser1.getUserId().toString());
            Order dcinfo = orderDao.getObject("Order.getOrderCountByDcid",order);
            if(dcinfo.getScore() != null) {
                sysUser1.setScore(dcinfo.getScore().toString());
            }
            if(dcinfo.getDiagnosetime() != null) {
                sysUser1.setDiagnosetime(dcinfo.getDiagnosetime());
            }
            if(dcinfo.getSumorder() != null) {
                sysUser1.setDiagnosecount(dcinfo.getSumorder());
            }


        }
        model.addAttribute("page", page);
        model.addAttribute("sysUser",sysUser);
        model.addAttribute("sysUserSession",sysUserSession);
        model.addAttribute("isfront",isfront);
        if(isall != null && isall.equals("1")){
            return "hs/alldoctorList";
        }
        return "hs/doctorList";


    }

    @RequestMapping("turnDoctorbelonged")
    public String turnDoctorbelonged(SysUser sysUser,Model model,String isall){
        List<Hospital> hospital = hospitalService.list("Hospital.getHospitalByBelongedid",sysUser);
        model.addAttribute("hospital",hospital);
        List<Hospital> department = hospitalService.list("Hospital.getDepartmentByBelongedid",sysUser);
        model.addAttribute("department",department);
        List<SysUser> doctor = sysUserService.list("SysUser.getDoctorNoBelonged",sysUser);
        model.addAttribute("doctor",doctor);

        model.addAttribute("sysUser",sysUser);
        model.addAttribute("isall",isall);
        return "hs/doctorTobelonged";

    }

    @RequestMapping("getBelongedid")
    public void getBelongedid(PrintWriter out,Hospital hospital){
        Hospital hospitalout = hospitalService.getObject("Hospital.getBelongedid",hospital);
        out.print(hospitalout.getHospitaldepartmentid());
        out.close();
    }

    @RequestMapping("saveDoctorBelonged")
    public void saveDoctorBelonged(PrintWriter out,SysUser sysUser){
        Boolean b = hospitalService.saveDoctorBelonged(sysUser);
        out.print(b);
        out.close();
    }

    @RequestMapping("deleteDoctorBelonged")
    public void deleteDoctorBelonged(PrintWriter out,SysUser sysUser){
        Boolean b = hospitalService.deleteDoctorBelonged(sysUser);
        out.print(b);
        out.close();
    }

    @RequestMapping("getDpByhs")
    @ResponseBody
    public String getDpByhs(Hospital hospital){
        List<Hospital> departments = hospitalService.list("Hospital.getDepartmentByHospitalid",hospital);

        JSONArray listArray=JSONArray.fromObject(departments);
        return listArray.toString();
    }
}